package com.zgh.leo.signin611.login_in;
//登录

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.zgh.leo.signin611.BaseActivity;
import com.zgh.leo.signin611.HttpThread1;
import com.zgh.leo.signin611.MainActivity;
import com.zgh.leo.signin611.R;

import static android.content.SharedPreferences.Editor;


public class Sign_in extends BaseActivity {


    private EditText id;//账号
    private EditText password;//密码

    private Button sign_in;//登录按钮
    private Button registered;//注册按钮

    private CheckBox RememberPassword;//记住密码
    private CheckBox autologin;//自动登陆

    private SharedPreferences setting;//保存数据
    private static final String PREPS_NAME="NamePwd";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        //初始化控件
        findView();

        //设置监听事件
        SetListener();

        //获取数据
        getData();
    }

    //初始化控件
    private void findView(){
        id = findViewById(R.id.id);
        password = findViewById(R.id.password);
        sign_in = findViewById(R.id.sign_in);
        registered = findViewById(R.id.registered);
        RememberPassword = findViewById(R.id.RememberPassword);
        autologin = findViewById(R.id.autologin);
    }

    //设置监听事件
    private void SetListener() {
        //登陆
        sign_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = id.getText().toString();
                String pass = password.getText().toString();

                //发送网络请求
                String url="http://192.168.1.111:8081/HTMLServlet";
                new HttpThread1(url,username,pass).start();


                if (username.equals("") && pass.equals("")){
                    //判断是否记住密码
                    if (RememberPassword.isChecked()){
                        setting = getSharedPreferences(PREPS_NAME,MODE_PRIVATE);
                        Editor edit= setting.edit();
                        edit.putBoolean("isKeep",true);
                        edit.putString("username",username);
                        edit.putString("password",pass);
                        edit.commit();
                    }
                    else{
                        setting = getSharedPreferences(PREPS_NAME,MODE_PRIVATE);
                        Editor edit= setting.edit();
                        edit.putBoolean("isKeep",true);
                        edit.putString("username","");
                        edit.putString("password","");
                        edit.commit();
                    }
                    Intent intent = new Intent(Sign_in.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                /*else{
                    Toast.makeText(Sign_in.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }*/
            }
        });

        //注册
        registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Sign_in.this, "不能注册哦！",
                        Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        //在界面显示数据之前得到之前存储的数据
        getData();
    }

    //获取数据
    private void getData() {
        setting = getSharedPreferences(PREPS_NAME,MODE_PRIVATE);
        if (setting.getBoolean("isKeep",false)){
            //如果保存过密码 则直接出现在编辑框内
            id.setText(setting.getString("username",""));
            password.setText(setting.getString("password",""));
        }
        else{
            //没有则设置为空
            id.setText("");
            password.setText("");
        }
    }

    //两次返回键推出App
    private long firstPressedTime;
    public void onBackPressed() {
        if (System.currentTimeMillis() - firstPressedTime < 2000) {
            super.onBackPressed();
        } else {
            Toast.makeText(Sign_in.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            firstPressedTime = System.currentTimeMillis();
        }
    }

}
